<template>
  <div class="app-container">
    <el-card>
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-input
            v-model="keyWord"
            placeholder="请输入想要查询的信息"
            @keyup.enter.native.prevent="searchDeptByName"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="searchDeptByName"
            >搜索</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="dialogAddVisible = true"
            icon="el-icon-plus"
            >添加</el-button
          >
          <el-dialog title="新增一个课程信息" :visible.sync="dialogAddVisible">
            <el-form @submit.native.prevent :model="deptInfo">
              <el-form-item label="学院编号">
                <el-input v-model="deptInfo.deptId"></el-input>
              </el-form-item>
              <el-form-item label="学院名称">
                <el-input v-model="deptInfo.deptName"></el-input>
              </el-form-item>

              <el-form-item label="地址">
                <el-input v-model="deptInfo.address"></el-input>
              </el-form-item>
              <el-form-item label="电话号码">
                <el-input v-model="deptInfo.telephone"></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogAddVisible = false">取 消</el-button>
              <el-button type="primary" @click="addDeptSubmit"
                >确 定</el-button
              >
            </div>
          </el-dialog>
        </el-form-item>
      </el-form>

      <!--student table-->
      <el-table
        v-loading="listLoading"
        :data="depts"
        element-loading-text="Loading"
        stripe
        height="550"
        border
        fits
        highlight-current-row
      >
        <el-table-column align="center" label="学院编号" prop="deptId" />
        <el-table-column label="学院名称" align="center">
          <template slot-scope="{ row }">
            <el-input v-if="row.isEdit" v-model="row.deptName" size="small" />
            <span v-else>{{row.deptName}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="地址">
          <template slot-scope="{ row }">
            <el-input v-if="row.isEdit" v-model="row.address" size="small" />
            <span v-else>{{row.address}}</span>
          </template>
        </el-table-column>
        <el-table-column label="联系方式" align="center">
          <template slot-scope="{ row }">
            <el-input v-if="row.isEdit" v-model="row.telephone" size="small" />
            <span v-else>{{row.telephone}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" prop="count">
          <template slot-scope="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="small"
              circle
              style="margin-left: 0"
              v-show="!row.isEdit"
              @click="row.isEdit = !row.isEdit"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-check"
              circle
              style="margin-left: 0"
              v-show="row.isEdit"
              size="small"
              @click="editOneStudent(row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="small"
              circle
              @click="deleteOneStudent(row.deptId)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import {
  getAllDept,
  addOneDept,
  updateOneDept,
  deleteOneDept,
  search,
} from "@/api/dept";
export default {
  data() {
    return {
      depts: null,
      depts: null,
      listLoading: true,
      keyWord: "",
      dialogAddVisible: false,
      deptInfo: {
        //学院信息
        deptId: "",
        deptName: "",
        address: "",
        telephone: "",
      },
    };
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getAllDept().then(({ data }) => {
        this.depts = data.map((v) => {
          this.$set(v, "isEdit", false);
          return v;
        });
        this.listLoading = false;
      });
    },
    addDeptSubmit() {
      this.dialogAddVisible = false;
      addOneDept(this.deptInfo).then(resp=>{
        this.$message({
          message:resp.msg,
          type: 'success'
        })
        this.depts.push(this.deptInfo);
      })
    },
    editOneStudent(row) {
      row.isEdit = false;
      updateOneDept(row).then(resp=>{
        if(resp.status == 200)
         this.$message({
          message:resp.msg,
          type: 'success'
        })
      })
    },
    deleteOneStudent(deptId) {
      if (confirm("你确定要删除这条数据吗？"))
        deleteOneDept(deptId).then(resp=>{
            this.$message({
            message:resp.msg,
            type: 'success'
          })
          this.depts = this.depts.filter(item=>{
            return item.deptId != deptId;
          })
        })
    },
    searchDeptByName() {
      this.listLoading = true;
      search(this.keyWord).then(({data}) => {
        this.depts = data.map((v) => {
          this.$set(v, "isEdit", false);
          return v;
        });
      });
      this.listLoading = false;
    },
  },
  created() {
    this.fetchData();
  },
};
</script>
